package com.zmn.plat.task.job.push;

import com.github.ltsopensource.core.domain.Action;
import com.github.ltsopensource.core.domain.Job;
import com.github.ltsopensource.spring.tasktracker.JobRunnerItem;
import com.github.ltsopensource.spring.tasktracker.LTS;
import com.github.ltsopensource.tasktracker.Result;
import com.zmn.common.dto.ResultDTO;
import com.zmn.common.dto2.ResponseDTO;
import com.zmn.manager.lts.interfaces.task.TaskManager;
import com.zmn.plat.business.interfaces.push.PushNoticeAppBService;
import com.zmn.plat.manager.utils.TaskConsts;
import lombok.extern.slf4j.Slf4j;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;

/**
 * 描述: 消息lts任务
 *
 * @author wangjie
 * @since 2020/3/31
 **/
@Slf4j
@LTS
public class PushLTSJob {

    @Resource
    PushNoticeAppBService pushNoticeAppBussService;

    @Resource
    TaskManager taskManager;

    /**
     * 描述: 消息推送定时任务， 重置启用状态
     *
     * @author wangjie
     * @since 2020/4/23
     **/
    @PostConstruct
    void init() {
        Job job = new Job();
        job.setTaskId(TaskConsts.TASK_ID_PUSH_NOTICE_RESET_STATUS);
        job.setParam(TaskManager.SHARD_FIELD, TaskConsts.TASK_ID_PUSH_NOTICE_RESET_STATUS);
        job.setCronExpression("0 0/1 * * * ?");
        job.setReplaceOnExist(true);
        ResultDTO result = taskManager.submitTask(job);
        if (!result.getStatusBool()) {
            log.error("提交任务成功 taskId:" + TaskConsts.TASK_ID_PUSH_NOTICE_RESET_STATUS);
        }
    }

    /**
     * 描述: 触发app 公告消息推送任务
     *
     * @param job 触发app 公告消息推送任务
     * @return com.github.ltsopensource.tasktracker.Result
     * @author wangjie
     * @since 2020/3/31
     **/
    @JobRunnerItem(shardValue = TaskConsts.TASK_ID_PUSH_NOTICE_MSG)
    public Result runPushNoticeMsg(Job job) {

        String noticeId = job.getParam("noticeId");
        String operator = job.getParam("operator");
        try {
            pushNoticeAppBussService.submitPushMsgToApp(Integer.valueOf(noticeId), operator);
            return new Result(Action.EXECUTE_SUCCESS, "执行成功了!");
        } catch (Exception e) {
            return new Result(Action.EXECUTE_FAILED, "执行失败.");
        }
    }

    /**
     * 描述: 重置过期数据状态
     *
     * @param job 任务
     * @return com.github.ltsopensource.tasktracker.Result
     * @author wangjie
     * @since 2020/4/23
     **/
    @JobRunnerItem(shardValue = TaskConsts.TASK_ID_PUSH_NOTICE_RESET_STATUS)
    public Result runPushNoticeResetStatusJon(Job job) {

        try {
          pushNoticeAppBussService.modifyBatchStatusToNo();
           return new Result(Action.EXECUTE_SUCCESS, "执行成功了!");
        } catch (Exception e) {
            return new Result(Action.EXECUTE_FAILED, "执行失败.");
        }
    }

}
